﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyLiveMesh.MVVMUtils;
using System.Collections.ObjectModel;
using MyLiveMesh.Views;
using System.ComponentModel;
using MyLiveMesh.Models;
using System.Collections.Generic;
using System.Linq;
using MyLiveMesh.ServiceReference2;

namespace MyLiveMesh.ViewModels
{
    public class WindowElement : INotifyPropertyChanged
    {
        private MDIWindow window;
        private string title;

        public MDIWindow Window { get { return window; } set { window = value; raisePropertychanged("Window"); } }
        public string Title { get { return title; } set { title = value; raisePropertychanged("Title"); } }


        public event PropertyChangedEventHandler PropertyChanged;

        public void raisePropertychanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

    public class DashboardViewModel : ViewModelBase, INavigableViewModel
    {
        private ObservableCollection<WindowElement> windows = new ObservableCollection<WindowElement>();

        public ObservableCollection<WindowElement> Windows { get { return windows; }}


        private ObservableCollection<string> friendList = new ObservableCollection<string>();

        public ObservableCollection<string> FriendList { get { return friendList; } }

        private ICommand maximiseCommand = null;
        private ICommand addFriendCommand = null;
        private ICommand deleteFriendCommand = null;

        private DirectoryTree directoryTree = new DirectoryTree();

        public DirectoryTree DirectoryTree { get { return directoryTree; } }
        
        private string friendName;

        public string FriendName { get { return friendName; } set { friendName = value; raisePropertyChanged("FriendName"); } }

        public ICommand MaximiseCommand
        {
            get
            {
            if (maximiseCommand == null)
                maximiseCommand = new RelayCommand<MDIWindow>((MDIWindow e) =>
                {
                    e.Maximise();
                });
            return maximiseCommand;
        } }

        public ICommand AddFriendCommand {
            get {
                if (addFriendCommand == null)
                    addFriendCommand = new RelayCommand<object>((object o) => {
                        Client.Current.addFriendAsync(friendName, friendName);
                    });
                return addFriendCommand;
            }
        }

        public ICommand DeleteFriendCommand
        {
            get
            {
                if (deleteFriendCommand == null)
                    deleteFriendCommand = new RelayCommand<string>((string o) =>
                    {
                        Client.Current.removeFriendAsync(o, o);
                    });
                return deleteFriendCommand;
            }
        }

        public DashboardViewModel()
        {
            MDIWindow window = new TreeviewPopup();
            window.Closing += new EventHandler<CancelEventArgs>(window_Closing);
            //Windows.Add(new WindowElement() { Window = window, Title = "test" });

            Client.Current.getDirectoryTreeCompleted += new EventHandler<ServiceReference2.getDirectoryTreeCompletedEventArgs>(Current_getDirectoryTreeCompleted);
            Client.Current.getFriendsCompleted += new EventHandler<getFriendsCompletedEventArgs>(Current_getFriendsCompleted);
            Client.Current.addFriendCompleted += new EventHandler<addFriendCompletedEventArgs>(Current_addFriendCompleted);
            Client.Current.removeFriendCompleted += new EventHandler<AsyncCompletedEventArgs>(Current_removeFriendCompleted);

            Client.Current.getDirectoryTreeAsync();
            Client.Current.getFriendsAsync();
        }

        void Current_removeFriendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            friendList.Remove((string)(e.UserState));
        }

        void Current_addFriendCompleted(object sender, addFriendCompletedEventArgs e)
        {
            if (e.Result)
            {
                FriendList.Add((string)(e.UserState));
            }
        }

        void Current_getFriendsCompleted(object sender, getFriendsCompletedEventArgs e)
        {
            friendList = new ObservableCollection<string>(e.Result);
            raisePropertyChanged("FriendList");
        }

        void Current_getDirectoryTreeCompleted(object sender, ServiceReference2.getDirectoryTreeCompletedEventArgs e)
        {
            directoryTree = e.Result;
            raisePropertyChanged("DirectoryTree");
        }

        void window_Closing(object sender, CancelEventArgs e)
        {
            MDIWindow window = (MDIWindow)sender;
            var w = windows.First((WindowElement we) => { return we.Window == window; });
            windows.Remove(w);
        }

        public INavigator Navigator { get; set; }
    }
}
